package com.c.yinyuezhushou.Dialog;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;

import com.c.yinyuezhushou.Fragment.MineFragment;
import com.c.yinyuezhushou.MRetrofit.MyObserver;
import com.c.yinyuezhushou.MRetrofit.MyRetrofit;
import com.c.yinyuezhushou.MRetrofit.PlayList;
import com.c.yinyuezhushou.MRetrofit.RetrofitService;
import com.c.yinyuezhushou.MRetrofit.getSongDetail;
import com.c.yinyuezhushou.MyApplication;
import com.c.yinyuezhushou.R;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class SongListDialog extends AlertDialog {
    private boolean cancelable;
    private MyApplication myApplication;
    private MineFragment mineFragment;
    private LoadingDialog loadingDialog;


    public SongListDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener, MineFragment mineFragment) {
        super(context, cancelable, cancelListener);
        this.cancelable = cancelable;
        myApplication = (MyApplication) getContext().getApplicationContext();
        this.mineFragment = mineFragment;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_song_list);
        setCancelable(cancelable);
        Window window = getWindow();
        window.setBackgroundDrawableResource(R.color.touming);
        window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

        WindowManager.LayoutParams params = window.getAttributes();

        params.gravity = Gravity.CENTER;

        window.setAttributes(params);

        EditText editText1 = findViewById(R.id.dsl_edit1);
        EditText editText2 = findViewById(R.id.dsl_edit2);

        Button button1 = findViewById(R.id.dsl_bu1);
        Button button2 = findViewById(R.id.dsl_bu2);
        Button button3 = findViewById(R.id.dsl_bu3);

        loadingDialog = new LoadingDialog(getContext(),false,null);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!StringUtils.isBlank(editText1.getText().toString())){
                    if (!mineFragment.getMyDButil().isSongList(editText1.getText().toString())){
                        mineFragment.setFSongList(editText1.getText().toString());
                        SongListDialog.this.dismiss();
                    }
                }else{
                    Toast.makeText(myApplication, "请输入名字", Toast.LENGTH_SHORT).show();
                }

            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!StringUtils.isBlank(editText1.getText().toString())){
                    if (!StringUtils.isBlank(editText2.getText().toString())){
                        if (!mineFragment.getMyDButil().isSongList(editText1.getText().toString())){
                            loadingDialog.show();
                            loadingDialog.setTitle("正在解析中...");
                            SongListDialog.this.hide();
                            long id = Long.parseLong(StringUtils.split(editText2.getText().toString(),"/")[3]);
                            MyRetrofit.getOb(myApplication, RetrofitService.class).getPlaylist(id)
                                    .subscribeOn(Schedulers.io())
                                    .observeOn(AndroidSchedulers.mainThread())
                                    .subscribe(new MyObserver<PlayList>() {
                                        @Override
                                        public void onNext(@io.reactivex.rxjava3.annotations.NonNull PlayList playList) {
                                            if (playList!=null){
                                                if (playList.getCode()==200){
                                                    List<PlayList.PlaylistBean.TrackIdsBean> idsBean = playList.getPlaylist().getTrackIds();
                                                    String idss="";
                                                    for (PlayList.PlaylistBean.TrackIdsBean ids : idsBean){
                                                        if (idsBean.indexOf(ids)==idsBean.size()-1){
                                                            idss = idss+ids.getId();
                                                        }else {
                                                            idss = idss+ids.getId()+",";
                                                        }
                                                    }
                                                    getSongDetail(idss,editText1.getText().toString());
                                                }
                                            }
                                        }

                                        @Override
                                        public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) {
                                            Toast.makeText(myApplication, "解析失败啊...", Toast.LENGTH_SHORT).show();
                                        }
                                    });
                        }else{
                            Toast.makeText(myApplication, "已有此歌单", Toast.LENGTH_SHORT).show();
                        }

                    }else {
                        Toast.makeText(myApplication, "请输入链接", Toast.LENGTH_SHORT).show();
                    }
                }else {
                    Toast.makeText(myApplication, "请输入名字", Toast.LENGTH_SHORT).show();
                }
            }
        });

        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SongListDialog.this.dismiss();
                loadingDialog.dismiss();
            }
        });
    }

    public void getSongDetail(String s,String name){
        MyRetrofit.getOb(myApplication, RetrofitService.class).getSongDetail(s)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new MyObserver<getSongDetail>() {
                    @Override
                    public void onNext(@io.reactivex.rxjava3.annotations.NonNull getSongDetail getSongDetail) {
                        loadingDialog.hide();
                        if (getSongDetail!=null){
                            if (getSongDetail.getCode()==200){
                                mineFragment.setSongList(s,name,getSongDetail);
                                SongListDialog.this.dismiss();
                            }
                        }
                    }

                    @Override
                    public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) {
                        Toast.makeText(myApplication, "歌单获取错误", Toast.LENGTH_SHORT).show();
                    }
                });
    }
}
